package com.example.demo.count;

/**
 * 描述
 *
 * @author: yuy
 */
public class TestCount {

    public static void main(String[] args) {
        // 计算 x^n
        long power = power(2, 5);
        System.out.println(power);
        long result = result(2, 5);
        System.out.println(result);

    }

    public static long power(long x, int n) {
        return n == 0 ? 1 : (n % 2) == 0 ? power(x, n >> 1) * power(x, n >> 1) : x * power(x, n - 1);
    }

    public static long result(long x, int n) {
        if (n == 0) return 1;
        if (n == 1) return x;
        int mid = n / 2;
        long r = result(x, mid);
        r = r * r;
        if (n % 2 == 1) {
            r *= x;
        }
        return r;
    }
}
